package com.yaorange.module.system.service.impl;

import com.yaorange.exception.ResourceNotFoundException;
import com.yaorange.module.system.mapper.SysRoleMapper;
import com.yaorange.module.system.service.SysRoleService;
import com.yaorange.pojo.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    SysRoleMapper sysRoleMapper;
    @Override
    public List<SysRole> findAll() {
        return sysRoleMapper.findAll();
    }

    @Override
    public SysRole findById(Long id) {
        return sysRoleMapper.findById(id);
    }

    @Override
    public void save(SysRole sysRole) {
        sysRoleMapper.save(sysRole);
    }

    @Override
    public void update(Long id, SysRole roleDetails) {
        roleDetails.setRoleId(id);
        sysRoleMapper.update(roleDetails);
    }

    @Override
    public void deleteById(Long id) {
        sysRoleMapper.deleteById(id);
    }

    @Override
    public List<Long> findPermissionsByRoleId(Long roleId) {
        return sysRoleMapper.findPermissionsByRoleId(roleId);
    }

    @Override
    public void grantPermission(List<Long> permissionIds, Long roleId) {
        if (roleId == null ||roleId == 0L){
            throw new ResourceNotFoundException("角色id不能为空");
        }
        sysRoleMapper.deleteByRoleId(roleId);
        if (permissionIds == null){
            throw new ResourceNotFoundException("权限id不能为空");
        }
        sysRoleMapper.grantPermissions(permissionIds, roleId);
    }

    @Override
    public void deleteByIds(List<Long> roleIds) {
        sysRoleMapper.deleteByIds(roleIds);
    }
}
